package cn.iocoder.yudao.module.base.service.info;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.general.controller.vo.DataStatisticsVO;
import cn.iocoder.yudao.module.gov.dal.mysql.resourcedirectory.ResourceDirectoryMapper;
import cn.iocoder.yudao.module.portal.controller.admin.vo.CataLogDataSetVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.directorybaseinfo.DirectoryBaseInfoDO;
import cn.iocoder.yudao.module.resource.dal.mysql.directorybaseinfo.DirectoryBaseInfoMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.base.controller.admin.info.vo.*;
import cn.iocoder.yudao.module.base.dal.dataobject.info.InfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.base.dal.mysql.info.InfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;

/**
 * 基础信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoMapper infoMapper;
    @Resource
    private DirectoryBaseInfoMapper directoryBaseInfoMapper;
    @Resource
    private ResourceDirectoryMapper resourceDirectoryMapper;
    @Override
    public Long createInfo(InfoSaveReqVO createReqVO) {
        // 插入
        InfoDO info = BeanUtils.toBean(createReqVO, InfoDO.class);
        infoMapper.insert(info);
        if(StringUtils.isNotBlank(createReqVO.getCatalogId())){
            // 处理目录关联
            Optional.ofNullable(createReqVO.getCatalogId())
                    .filter(StringUtils::isNotBlank)
                    .ifPresent(catalogId ->
                            directoryBaseInfoMapper.insert(
                                    DirectoryBaseInfoDO.builder()
                                            .catalogId(Long.parseLong(catalogId))
                                            .baseId(info.getId())
                                            .build()
                            )
                    );
        }
        // 返回
        return info.getId();
    }

    @Override
    public void updateInfo(InfoSaveReqVO updateReqVO) {
        // 参数校验
        Objects.requireNonNull(updateReqVO, "更新参数不能为空");

        // 校验存在
        validateInfoExists(updateReqVO.getId());

        // 更新主信息
        InfoDO updateObj = BeanUtils.toBean(updateReqVO, InfoDO.class);
        infoMapper.updateById(updateObj);

        // 处理目录关联
        Optional.ofNullable(updateReqVO.getCatalogId())
                .filter(StringUtils::isNotBlank)
                .ifPresent(catalogId ->
                        directoryBaseInfoMapper.insertOrUpdate(
                                DirectoryBaseInfoDO.builder()
                                        .catalogId(Long.parseLong(catalogId))
                                        .baseId(updateObj.getId())
                                        .build()
                        )
                );
    }

    @Override
    public void deleteInfo(Long id) {
        // 校验存在
        validateInfoExists(id);
        // 删除
        infoMapper.deleteById(id);
    }

    @Override
        public void deleteInfoListByIds(List<Long> ids) {
        // 删除
        infoMapper.deleteByIds(ids);
        }


    private void validateInfoExists(Long id) {
        if (infoMapper.selectById(id) == null) {
            throw exception(new ErrorCode(500,"数据不存在"));
        }
    }

    @Override
    public InfoDO getInfo(Long id) {
        return infoMapper.selectByIdPro(id);
    }

    @Override
    public PageResult<InfoDO> getInfoPage(InfoPageReqVO reqVO) {
        IPage<InfoDO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        infoMapper.selectPagePro(page, reqVO);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    @Override
    public DataStatisticsVO getDataStatistics() {
        //查询数据目录总数
        Long dataDirectoryNumber = resourceDirectoryMapper.selectCount();
        //查询数据集
        //数据接口
        //数据项
        //总数据
        DataStatisticsVO dataStatisticsVO = new DataStatisticsVO();
        dataStatisticsVO.setDataDirectoryNumber(String.valueOf(dataDirectoryNumber))
                .setDataItemNumber("0")
                .setDataInterfaceNumber("0")
                .setDataItemNumber("0")
                .setDataTotalNumber("0");
        return dataStatisticsVO;
    }

    @Override
    public InfoDO getInterfaceByCatalogId(Long catalogId) {
        return infoMapper.getInterfaceByCatalogId(catalogId);
    }

    @Override
    public List<CataLogDataSetVO> getDataSetById(Long catalogId, String dataSourceId) {
        return infoMapper.getDataSetById(catalogId,dataSourceId);
    }

    @Override
    public void addDataSet(CataLogDataSetVO dataSetVO1) {
        infoMapper.addDataSet(dataSetVO1);
    }

    @Override
    public void updateDownloadCount(CataLogDataSetVO cataLogDataSetVO) {
        infoMapper.updateDownloadCount(cataLogDataSetVO);
    }

    @Override
    public String applicationStatus(Long portalLoginUserId, Long catalogId, String applicationType) {
        return infoMapper.applicationStatus(portalLoginUserId,catalogId,applicationType);
    }

    @Override
    public int getDsCount(Long id) {
        return infoMapper.getDsCount(id);
    }

    @Override
    public int portCount(Long id) {
        return infoMapper.getPortCount(id);
    }

    @Override
    public Boolean getIsPort(Long id) {
        return infoMapper.getIsPort(id);
    }

    @Override
    public Integer getCatalogCount() {
        return infoMapper.getCatalogCount();
    }

    @Override
    public Integer getInterfaceCount() {
        return infoMapper.getInterfaceCount();
    }

    @Override
    public Integer getApplicationCount() {
        return infoMapper.getApplicationCount();
    }

    @Override
    public Integer getCatalogCollectCount() {
        return infoMapper.getCatalogCollectCount();
    }

}